{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Philosopher Chatting\n",
    "\n",
    "Intro: Famous Philosopher chatting on a topic, including Confucius, Socrates and Aristotle\n",
    "\n",
    "## Steps:\n",
    "- Build agents to represent Confucius, Socrates and Aristotle\n",
    "\n",
    "- Build a manager agent to form a group of them\n",
    "\n",
    "- Give a topic and let them chat !"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# defining the educator class for chat\n",
    "from typing import List\n",
    "from agentlite.agents import ABCAgent, BaseAgent\n",
    "from agentlite.llm.agent_llms import BaseLLM, get_llm_backend\n",
    "from agentlite.llm.LLMConfig import LLMConfig\n",
    "from agentlite.logging.multi_agent_log import AgentLogger\n",
    "from agentlite.actions.BaseAction import BaseAction\n",
    "from env import openai_api_key\n",
    "\n",
    "\n",
    "## using the agentrool logger to record the running log\n",
    "agent_logger = AgentLogger(PROMPT_DEBUG_FLAG=False)\n",
    "\n",
    "# Define the Philosopher class \n",
    "class Philosopher(BaseAgent):\n",
    "    def __init__(\n",
    "        self,\n",
    "        philosopher,\n",
    "        llm: BaseLLM,\n",
    "        actions: List[BaseAction] = [], \n",
    "        manager: ABCAgent = None,\n",
    "        **kwargs\n",
    "    ):\n",
    "        name = philosopher\n",
    "        role = f\"\"\"You are {philosopher}, the famous educator in history. You are very familiar with {philosopher}'s Book and Thought. Tell your opinion on behalf of {philosopher}.\"\"\"\n",
    "        super().__init__(\n",
    "            name=name,\n",
    "            role=role,\n",
    "            llm=llm,\n",
    "            actions=actions,\n",
    "            manager=manager,\n",
    "            logger=agent_logger,\n",
    "        )\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the backend LLM and Confucius, Socrates, Aristotle\n",
    "llm_config_dict = {\"llm_name\": \"gpt-4-32k\", \"temperature\": 0.0, \"openai_api_key\": openai_api_key}\n",
    "llm_config = LLMConfig(llm_config_dict)\n",
    "llm = get_llm_backend(llm_config)\n",
    "\n",
    "Confucius = Philosopher(philosopher= \"Confucius\", llm = llm)\n",
    "Socrates = Philosopher(philosopher=\"Socrates\", llm = llm)\n",
    "Aristotle = Philosopher(philosopher=\"Aristotle\", llm = llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentlite.commons import AgentAct, TaskPackage\n",
    "from agentlite.actions import ThinkAct, FinishAct\n",
    "from agentlite.actions.InnerActions import INNER_ACT_KEY\n",
    "from agentlite.agents.agent_utils import AGENT_CALL_ARG_KEY\n",
    "\n",
    "# Add an illustration example for the philosopher agent\n",
    "exp_task = \"What do you think the meaning of life?\"\n",
    "exp_task_pack = TaskPackage(instruction=exp_task)\n",
    "\n",
    "act_1 = AgentAct(\n",
    "    name=ThinkAct.action_name,\n",
    "    params={INNER_ACT_KEY: f\"\"\"Based on my thought, we are born to live a meaningful life, and it is in living a meaningful life that our existence gains value. Even if a life is brief, if it holds value, it is meaningful. A life without value is merely existence, a mere survival, a walking corpse.\"\"\"\n",
    "    },\n",
    ")\n",
    "obs_1 = \"OK. I have finished my thought, I can pass it to the manager now.\"\n",
    "\n",
    "act_2 = AgentAct(name=FinishAct.action_name, params={INNER_ACT_KEY: \"I can summarize my thought now.\"})\n",
    "obs_2 = \"I finished my task, I think the meaning of life is to pursue value for the whold world.\"\n",
    "exp_act_obs = [(act_1, obs_1), (act_2, obs_2)]\n",
    "\n",
    "Confucius.prompt_gen.add_example(\n",
    "    task = exp_task_pack, action_chain = exp_act_obs\n",
    ")\n",
    "Socrates.prompt_gen.add_example(\n",
    "    task = exp_task_pack, action_chain = exp_act_obs\n",
    ")\n",
    "Aristotle.prompt_gen.add_example(\n",
    "    task = exp_task_pack, action_chain = exp_act_obs\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define manager agent\n",
    "from agentlite.agents import ManagerAgent\n",
    "\n",
    "manager_agent_info = {\n",
    "    \"name\": \"manager_agent\",\n",
    "    \"role\": \"you are managing Confucius, Socrates and Aristotle to discuss on questions. Ask their opinion one by one and summarize their view of point.\"\n",
    "}\n",
    "team = [Confucius, Socrates, Aristotle]\n",
    "manager_agent = ManagerAgent(name=manager_agent_info[\"name\"], role=manager_agent_info[\"role\"], llm=llm, TeamAgents=team,logger=agent_logger)\n",
    "\n",
    "# add illustration example for the manager agent\n",
    "\n",
    "exp_task = \"What is the meaning of life?\"\n",
    "exp_task_pack = TaskPackage(instruction=exp_task)\n",
    "\n",
    "act_1 = AgentAct(\n",
    "    name=ThinkAct.action_name,\n",
    "    params={INNER_ACT_KEY: f\"\"\"I can ask Confucius, Socrates and Aristotle one by one on their thoughts, and then summary the opinion myself.\"\"\"\n",
    "    },\n",
    ")\n",
    "obs_1 = \"OK.\"\n",
    "\n",
    "\n",
    "act_2 = AgentAct(\n",
    "    name=Confucius.name,\n",
    "    params={AGENT_CALL_ARG_KEY: \"What is your opinion on the meaning of life?\",\n",
    "        },\n",
    ")\n",
    "obs_2 = \"\"\"Based on my thought, I think the meaning of life is to pursue value for the whold world.\"\"\"\n",
    "\n",
    "\n",
    "act_3 = AgentAct(\n",
    "    name=ThinkAct.action_name,\n",
    "    params={INNER_ACT_KEY: f\"\"\"I have obtained information from Confucius, I need to collect more information from Socrates.\"\"\"\n",
    "    },\n",
    ")\n",
    "obs_3 = \"OK.\"\n",
    "\n",
    "act_4 = AgentAct(\n",
    "    name=Socrates.name,\n",
    "    params={AGENT_CALL_ARG_KEY: \"What is your opinion on the meaning of life?\",\n",
    "        },\n",
    ")\n",
    "obs_4 = \"\"\"I think the meaning of life is finding happiness.\"\"\"\n",
    "\n",
    "act_5 = AgentAct(\n",
    "    name=ThinkAct.action_name,\n",
    "    params={INNER_ACT_KEY: f\"\"\"I have obtained information from Confucius and Socrates, I can collect more information from Aristotle.\"\"\"\n",
    "    },\n",
    ")\n",
    "obs_5 = \"OK.\"\n",
    "\n",
    "act_6 = AgentAct(\n",
    "    name=Aristotle.name,\n",
    "    params={AGENT_CALL_ARG_KEY: \"What is your opinion on the meaning of life?\",\n",
    "        },\n",
    ")\n",
    "obs_6 = \"\"\"I believe the freedom of spirit is the meaning.\"\"\"\n",
    "\n",
    "\n",
    "act_7 = AgentAct(name=FinishAct.action_name, params={INNER_ACT_KEY: \"Their thought on the meaning of life is to pursue value, happiniss and freedom of spirit.\"})\n",
    "obs_7 = \"Task Completed. The meaning of life is to pursue value, happiness and freedom of spirit.\"\n",
    "\n",
    "exp_act_obs = [(act_1, obs_1), (act_2, obs_2), (act_3, obs_3), (act_4, obs_4), (act_5, obs_5), (act_6, obs_6), (act_7, obs_7)]\n",
    "\n",
    "manager_agent.prompt_gen.add_example(\n",
    "    task = exp_task_pack, action_chain = exp_act_obs\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Agent \u001b[94mmanager_agent\u001b[0m receives the following \u001b[4mTaskPackage\u001b[0m:\n",
      "\u001b[96m[\n",
      "\tTask ID: c48b516e-bd4d-40c2-a537-54b9742a7fb9\n",
      "\tInstruction: What should we pursue during our life?\n",
      "]\u001b[0m\n",
      "====\u001b[94mmanager_agent\u001b[0m starts execution on TaskPackage c48b516e-bd4d-40c2-a537-54b9742a7fb9====\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/liangwei.yang/miniconda3/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Agent \u001b[94mmanager_agent\u001b[0m takes 0-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Think\n",
      "\tparams: {'response': 'I can ask Confucius, Socrates and Aristotle one by one on their thoughts, and then summary the opinion myself.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mOK\u001b[0m\n",
      "Agent \u001b[94mmanager_agent\u001b[0m takes 1-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Confucius\n",
      "\tparams: {'Task': 'What is your opinion on what we should pursue during our life?'}\n",
      "}\u001b[0m\n",
      "Agent \u001b[94mConfucius\u001b[0m receives the following \u001b[4mTaskPackage\u001b[0m:\n",
      "\u001b[96m[\n",
      "\tTask ID: c48b516e-bd4d-40c2-a537-54b9742a7fb9\n",
      "\tInstruction: What is your opinion on what we should pursue during our life?\n",
      "]\u001b[0m\n",
      "====\u001b[94mConfucius\u001b[0m starts execution on TaskPackage c48b516e-bd4d-40c2-a537-54b9742a7fb9====\n",
      "Agent \u001b[94mConfucius\u001b[0m takes 0-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Think\n",
      "\tparams: {'response': 'Confucius believed that the purpose of life is to cultivate oneself and to constantly improve oneself in order to achieve moral perfection. He emphasized self-cultivation and moral education, and believed that by practicing virtue, one can achieve harmony with oneself, others, and the world.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mOK\u001b[0m\n",
      "Agent \u001b[94mConfucius\u001b[0m takes 1-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Finish\n",
      "\tparams: {'response': 'I think Confucius would say that we should pursue moral perfection and harmony by constantly improving ourselves through self-cultivation and moral education.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mTask Completed.\u001b[0m\n",
      "=========\u001b[94mConfucius\u001b[0m finish execution. TaskPackage[ID:c48b516e-bd4d-40c2-a537-54b9742a7fb9] status:\n",
      "\u001b[96m[\n",
      "\tcompletion: completed\n",
      "\tanswer: I think Confucius would say that we should pursue moral perfection and harmony by constantly improving ourselves through self-cultivation and moral education.\n",
      "]\u001b[0m\n",
      "==========\n",
      "Observation: \u001b[92mI think Confucius would say that we should pursue moral perfection and harmony by constantly improvi[TLDR]\u001b[0m\n",
      "Agent \u001b[94mmanager_agent\u001b[0m takes 2-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Think\n",
      "\tparams: {'response': 'I have obtained information from Confucius, I need to collect more information from Socrates.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mOK\u001b[0m\n",
      "Agent \u001b[94mmanager_agent\u001b[0m takes 3-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Socrates\n",
      "\tparams: {'Task': 'What is your opinion on what we should pursue during our life?'}\n",
      "}\u001b[0m\n",
      "Agent \u001b[94mSocrates\u001b[0m receives the following \u001b[4mTaskPackage\u001b[0m:\n",
      "\u001b[96m[\n",
      "\tTask ID: c48b516e-bd4d-40c2-a537-54b9742a7fb9\n",
      "\tInstruction: What is your opinion on what we should pursue during our life?\n",
      "]\u001b[0m\n",
      "====\u001b[94mSocrates\u001b[0m starts execution on TaskPackage c48b516e-bd4d-40c2-a537-54b9742a7fb9====\n",
      "Agent \u001b[94mSocrates\u001b[0m takes 0-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Think\n",
      "\tparams: {'response': 'As Socrates said, the unexamined life is not worth living. We should spend our lives pursuing knowledge, wisdom, and self-improvement. Only by constantly questioning ourselves and the world around us can we truly live a meaningful life.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mOK\u001b[0m\n",
      "Agent \u001b[94mSocrates\u001b[0m takes 1-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Finish\n",
      "\tparams: {'response': \"Based on Socrates's teachings, we should pursue knowledge, wisdom, and self-improvement. This is the key to living a meaningful life.\"}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mTask Completed.\u001b[0m\n",
      "=========\u001b[94mSocrates\u001b[0m finish execution. TaskPackage[ID:c48b516e-bd4d-40c2-a537-54b9742a7fb9] status:\n",
      "\u001b[96m[\n",
      "\tcompletion: completed\n",
      "\tanswer: Based on Socrates's teachings, we should pursue knowledge, wisdom, and self-improvement. This is the key to living a meaningful life.\n",
      "]\u001b[0m\n",
      "==========\n",
      "Observation: \u001b[92mBased on Socrates's teachings, we should pursue knowledge, wisdom, and self-improvement. This is the[TLDR]\u001b[0m\n",
      "Agent \u001b[94mmanager_agent\u001b[0m takes 4-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Think\n",
      "\tparams: {'response': 'I have obtained information from Confucius and Socrates, I can collect more information from Aristotle.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mOK\u001b[0m\n",
      "Agent \u001b[94mmanager_agent\u001b[0m takes 5-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Aristotle\n",
      "\tparams: {'Task': 'What is your opinion on what we should pursue during our life?'}\n",
      "}\u001b[0m\n",
      "Agent \u001b[94mAristotle\u001b[0m receives the following \u001b[4mTaskPackage\u001b[0m:\n",
      "\u001b[96m[\n",
      "\tTask ID: c48b516e-bd4d-40c2-a537-54b9742a7fb9\n",
      "\tInstruction: What is your opinion on what we should pursue during our life?\n",
      "]\u001b[0m\n",
      "====\u001b[94mAristotle\u001b[0m starts execution on TaskPackage c48b516e-bd4d-40c2-a537-54b9742a7fb9====\n",
      "Agent \u001b[94mAristotle\u001b[0m takes 0-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Think\n",
      "\tparams: {'response': \"Based on Aristotle's thought, humans should pursue eudaimonia, or human flourishing, as the ultimate goal of life.\"}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mOK\u001b[0m\n",
      "Agent \u001b[94mAristotle\u001b[0m takes 1-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Finish\n",
      "\tparams: {'response': 'In conclusion, Aristotle believes that we should pursue human flourishing, or eudaimonia, as the ultimate goal of life. This means living a life that is virtuous, rational, and fulfilling.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mTask Completed.\u001b[0m\n",
      "=========\u001b[94mAristotle\u001b[0m finish execution. TaskPackage[ID:c48b516e-bd4d-40c2-a537-54b9742a7fb9] status:\n",
      "\u001b[96m[\n",
      "\tcompletion: completed\n",
      "\tanswer: In conclusion, Aristotle believes that we should pursue human flourishing, or eudaimonia, as the ultimate goal of life. This means living a life that is virtuous, rational, and fulfilling.\n",
      "]\u001b[0m\n",
      "==========\n",
      "Observation: \u001b[92mIn conclusion, Aristotle believes that we should pursue human flourishing, or eudaimonia, as the ult[TLDR]\u001b[0m\n",
      "Agent \u001b[94mmanager_agent\u001b[0m takes 6-step \u001b[4mAction\u001b[0m:\n",
      "\u001b[94m{\n",
      "\tname: Finish\n",
      "\tparams: {'response': 'Their thoughts on what we should pursue during our life are: moral perfection and harmony, knowledge and self-improvement, and human flourishing.'}\n",
      "}\u001b[0m\n",
      "Observation: \u001b[92mTask Completed.\u001b[0m\n",
      "=========\u001b[94mmanager_agent\u001b[0m finish execution. TaskPackage[ID:c48b516e-bd4d-40c2-a537-54b9742a7fb9] status:\n",
      "\u001b[96m[\n",
      "\tcompletion: completed\n",
      "\tanswer: Their thoughts on what we should pursue during our life are: moral perfection and harmony, knowledge and self-improvement, and human flourishing.\n",
      "]\u001b[0m\n",
      "==========\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Their thoughts on what we should pursue during our life are: moral perfection and harmony, knowledge and self-improvement, and human flourishing.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "from agentlite.commons import AgentAct, TaskPackage\n",
    "\n",
    "exp_task = \"What should we pursue during our life?\"\n",
    "exp_task_pack = TaskPackage(instruction=exp_task)\n",
    "manager_agent(exp_task_pack)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
